/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public abstract class ListPage extends AbstractPage {

	protected Table table;
	
	private boolean packingColumns = false;

	public ListPage(String title, String image) {
		super(title, image);
	}

	protected Control createContent(Composite parent) {
		final Composite comp = new Composite(parent, SWT.NONE);
		final Control entry = createEntry(comp);
		entry.pack();
		final Button add = new Button(comp, SWT.PUSH);
		add.setText("Ajouter");
		add.pack();
		final Button remove = new Button(comp, SWT.PUSH);
		remove.setText("Supprimer");
		remove.pack();
		table = new Table(comp, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
		initTable();
		// layout
		FormLayout layout = new FormLayout();
		layout.marginTop = 10;
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginBottom = 10;
		comp.setLayout(layout);
		FormData fd = new FormData();
		fd.top = new FormAttachment(0);
		fd.left = new FormAttachment(0);
		fd.right = new FormAttachment(add, -10);
		entry.setLayoutData(fd);
		fd = new FormData();
		fd.top = new FormAttachment(0);
		fd.right = new FormAttachment(remove, -20);
		add.setLayoutData(fd);
		fd = new FormData();
		fd.top = new FormAttachment(0);
		fd.right = new FormAttachment(100);
		remove.setLayoutData(fd);
		fd = new FormData();
		fd.top = new FormAttachment(entry, 10);
		fd.left = new FormAttachment(0);
		fd.right = new FormAttachment(100);
		fd.bottom = new FormAttachment(100);
		table.setLayoutData(fd);
		// listeners
		add.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				add();
			}
		});
		remove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				remove();
			}
		});
		table.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				packColumns();
			}
		});
		return comp;
	}

	protected abstract Control createEntry(Composite parent);

	/**
	 * Initialise des colonnes. Destiné à être appelé de initTable.
	 * 
	 * @param titles
	 * @param styles
	 */
	protected void initColumns(String[] titles, int[] styles) {
		table.setHeaderVisible(true);
		for (int i = 0; i < titles.length; i++) {
			TableColumn col = new TableColumn(table, styles[i]);
			col.setText(titles[i]);
		}
	}

	protected void initTable() {
	}

	protected void packColumns() {
		if(packingColumns)
			return;
		packingColumns = true;
		packColumns(table.getClientArea().width);
		packingColumns = false;
	}

	/**
	 * Corrige la taille des colonnes. Destiné à être surchargé.
	 * <p>
	 * Implémentation par défaut: pack de toutes les colonnes
	 * 
	 * @param width taille totale
	 */
	protected void packColumns(int width) {
		for (TableColumn col : table.getColumns())
			col.pack();
	}

	protected abstract void add();

	protected abstract void remove();

}
